package cn.com.wangluotongxin.mall.service.impl;

import cn.com.wangluotongxin.mall.dto.GoodsMxDto;
import cn.com.wangluotongxin.mall.model.MallGoodsInfo;
import cn.com.wangluotongxin.mall.model.MallGoodsMx;
import cn.com.wangluotongxin.mall.mapper.MallGoodsMxMapper;
import cn.com.wangluotongxin.mall.model.MallGoodsStandard;
import cn.com.wangluotongxin.mall.service.IMallGoodsMxService;
import cn.com.wangluotongxin.mall.vo.GoodsMxVo;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 网络通信-商城APP-商品明细 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-11-05
 */
@Service
public class MallGoodsMxServiceImpl extends ServiceImpl<MallGoodsMxMapper, MallGoodsMx> implements IMallGoodsMxService {

    @Override
    public IPage<GoodsMxVo> getPage(GoodsMxDto dto) {
        Page page = Page.of(dto.getPageNumber(), dto.getPageSize());
        return baseMapper.getPage(page, dto);
    }

    @Override
    public MallGoodsMx getByGoodsStandardId(Integer mallGoodsId, Integer mallStandardId) {
        return this.getOne(new LambdaQueryWrapper<MallGoodsMx>()
                .eq(MallGoodsMx::getMallGoodsId, mallGoodsId)
                .eq(MallGoodsMx::getMallStandardId, mallStandardId)
                .last("limit 0,1"));
    }

    @Override
    public void goodsMxSync(MallGoodsInfo goodsInfo, MallGoodsStandard goodsStandard) {
        MallGoodsMx goodsMx = this.get(goodsInfo, goodsStandard);
        if(goodsMx.getId() != null && goodsMx.getId() > 0) {
            this.updateById(goodsMx);

        } else {
            this.save(goodsMx);
        }
    }

    private MallGoodsMx get(MallGoodsInfo goodsInfo, MallGoodsStandard goodsStandard) {
        MallGoodsMx newGoodsMx = new MallGoodsMx();

        MallGoodsMx goodsMx = this.getByGoodsStandardId(goodsInfo.getId(), goodsStandard.getId());
        if(goodsMx != null) {
            newGoodsMx.setId(goodsMx.getId());
        }

        newGoodsMx.setTenantId(goodsInfo.getTenantId());
        newGoodsMx.setTenantName(goodsInfo.getTenantName());
        newGoodsMx.setMallGoodsId(goodsInfo.getId());
        newGoodsMx.setMallStandardId(goodsStandard.getId());
        newGoodsMx.setGoodsId(goodsInfo.getGoodsId());
        newGoodsMx.setGoodsNo(goodsInfo.getGoodsNo());
        newGoodsMx.setGoodsName(goodsInfo.getGoodsName());
        newGoodsMx.setQuickQueryId(goodsInfo.getQuickQueryId());
        newGoodsMx.setQuickQueryName(goodsInfo.getQuickQueryName());
        newGoodsMx.setCatalogId(goodsInfo.getCatalogId());
        newGoodsMx.setCatalogPath(goodsInfo.getCatalogPath());
        newGoodsMx.setCatalogName(goodsInfo.getCatalogName());
        newGoodsMx.setGoodsImages(goodsInfo.getGoodsImages());
        newGoodsMx.setStandardId(goodsStandard.getStandardId());
        newGoodsMx.setStandardName(goodsStandard.getStandardName());
        newGoodsMx.setColorId(goodsStandard.getColorId());
        newGoodsMx.setColorName(goodsStandard.getColorName());
        newGoodsMx.setSizeId(goodsStandard.getSizeId());
        newGoodsMx.setSizeName(goodsStandard.getSizeName());
        newGoodsMx.setUnitId(goodsStandard.getUnitId());
        newGoodsMx.setUnitName(goodsStandard.getUnitName());
        newGoodsMx.setOriPrice(goodsStandard.getOriPrice());
        newGoodsMx.setSalePrice(goodsStandard.getSalePrice());
        newGoodsMx.setStockNumber(goodsStandard.getStockNumber());
        newGoodsMx.setSaleNumber(goodsStandard.getSaleNumber());
        newGoodsMx.setGoodsRemark(goodsInfo.getGoodsRemark());
        newGoodsMx.setStandardRemark(goodsStandard.getStandardRemark());
        newGoodsMx.setStandardImages(goodsStandard.getStandardImages());
        newGoodsMx.setIsSale(goodsStandard.getIsSale());
        if(StrUtil.isNotBlank(goodsStandard.getStandardImages())) {
            List<String> standardImages = Arrays.asList(goodsStandard.getStandardImages().split(","));
            if(ArrayUtil.isAllNotEmpty(standardImages)) {
                newGoodsMx.setIndexImage(standardImages.get(0));
            }
        } else if(StrUtil.isNotBlank(goodsInfo.getGoodsImages())) {
            List<String> goodsImages = Arrays.asList(goodsInfo.getGoodsImages().split(","));
            if(ArrayUtil.isAllNotEmpty(goodsImages)) {
                newGoodsMx.setIndexImage(goodsImages.get(0));
            }
        }
        return newGoodsMx;
    }
}
